package org.xlp.bean.base;

import org.xlp.assertion.AssertUtils;
import org.xlp.bean.exception.BeanBaseException;
import org.xlp.utils.ClassLoaderUtils;

/**
 * bean定义创建器接口
 */
@FunctionalInterface
public interface IBeanDefinitionCreator {
    /**
     * 根据给定的bean创建对应的bean定义
     * @param beanClass bean类型
     * @return
     */
    IBeanDefinition createBeanDefinition(Class<?> beanClass);

    /**
     * 根据给定的bean类型名称创建对应的bean定义
     * @param beanClassName bean类型名称
     * @param classLoader 类加载器
     * @return
     * @throws NullPointerException 假如第一个参数为null，则抛出该异常
     * @throws BeanBaseException 假如创建失败，则抛出该异常
     */
    default IBeanDefinition createBeanDefinition(String beanClassName, ClassLoader classLoader){
        AssertUtils.isNotNull(beanClassName, "beanClassName parameter must not be null!");
        classLoader = classLoader == null ? ClassLoaderUtils.getDefaultClassLoader() : classLoader;
        try {
            return createBeanDefinition(Class.forName(beanClassName, true, classLoader));
        } catch (ClassNotFoundException e) {
            throw new BeanBaseException(e);
        }
    }
    /**
     * 根据给定的bean类型名称创建对应的bean定义
     * @param beanClassName bean类型名称
     * @return
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    default IBeanDefinition createBeanDefinition(String beanClassName){
        AssertUtils.isNotNull(beanClassName, "beanClassName parameter must not be null!");
        return createBeanDefinition(beanClassName, IBeanDefinition.class.getClassLoader());
    }
}
